﻿namespace WMS6.Web.CustomizeReport
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.CustomizeReportServiceRef;
    using WMS6.Web.UserControls;

    public class TNTDailyInvRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F16011;
        protected GridView gvDailyInv;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected Label lblMsg;
        protected Label lblReceiptInquiryList;
        protected Label lblTotalQty;
        protected Label lblTotalQtyText;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        private decimal TotalQty;
        protected UpdatePanel up1;

        public void BindData2GridView(InventoryQueryEntity inventoryQueryEntity)
        {
            this.PagerFooter.CurrentPage = int.Parse(inventoryQueryEntity.CurrentPage);
            DataSet ds = CustomizeReportServiceClientProxy.GetInstance().GetTNTDailyInventoryRptDataSet(inventoryQueryEntity);
            base.BindData2GridView(this.gvDailyInv, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            inventoryQueryEntity.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            string currentPageIndex = inventoryQueryEntity.CurrentPage;
            inventoryQueryEntity.CurrentPage = "0";
            this.gvDailyInv.Columns[1].Visible = false;
            this.gvDailyInv.AllowSorting = false;
            this.BindData2GridView(inventoryQueryEntity);
            this.gvDailyInv.ShowFooter = false;
            inventoryQueryEntity.IsGetAll = false;
            inventoryQueryEntity.CurrentPage = currentPageIndex;
            this.SetInventoryQueryEntity(inventoryQueryEntity);
            base.Export2Excel("DailyInv.xls", this.gvDailyInv);
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            inventoryQueryEntity.CurrentPage = "0";
            this.BindData2GridView(inventoryQueryEntity);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            inventoryQueryEntity.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2GridView(inventoryQueryEntity);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            inventoryQueryEntity.OwnerID = "";
            inventoryQueryEntity.SkuClassID = "";
            inventoryQueryEntity.Sku = "";
            inventoryQueryEntity.Descr = "";
            inventoryQueryEntity.SkuGroup = "";
            inventoryQueryEntity.ExpiryDate = "";
            this.BindData2GridView(inventoryQueryEntity);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            if (int.Parse(inventoryQueryEntity.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                inventoryQueryEntity.CurrentPage = (int.Parse(inventoryQueryEntity.CurrentPage) + 1).ToString();
                this.BindData2GridView(inventoryQueryEntity);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            if (int.Parse(inventoryQueryEntity.CurrentPage) > 0)
            {
                inventoryQueryEntity.CurrentPage = (int.Parse(inventoryQueryEntity.CurrentPage) - 1).ToString();
                this.BindData2GridView(inventoryQueryEntity);
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
            this.BindData2GridView(inventoryQueryEntity);
        }

        private InventoryQueryEntity GetInventoryQueryEntity()
        {
            return (base.PageQueryEntitySession as InventoryQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
                inventoryQueryEntity.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2GridView(inventoryQueryEntity);
            }
        }

        protected void gvDailyInv_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F16011", this.gvDailyInv);
        }

        protected void gvDailyInv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                this.lblTotalQtyText.Text = base.GetPreferenceDigit(this.TotalQty);
            }
        }

        protected void gvDailyInv_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                InventoryQueryEntity inventoryQueryEntity = this.GetInventoryQueryEntity();
                inventoryQueryEntity.SortField = e.SortExpression;
                if (inventoryQueryEntity.SortDirection == "ASC")
                {
                    inventoryQueryEntity.SortDirection = "DESC";
                }
                else if (inventoryQueryEntity.SortDirection == "DESC")
                {
                    inventoryQueryEntity.SortDirection = "ASC";
                }
                this.BindData2GridView(inventoryQueryEntity);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            InventoryQueryEntity inventoryQueryEntity = new InventoryQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    inventoryQueryEntity.OwnerID = this.PreviousPage.OwnerID;
                    inventoryQueryEntity.SkuClassDescr = this.PreviousPage.SkuClass;
                    inventoryQueryEntity.Sku = this.PreviousPage.SkuID;
                    inventoryQueryEntity.Descr = this.PreviousPage.SkuDescr;
                    inventoryQueryEntity.SkuGroup = this.PreviousPage.SkuGroup;
                    inventoryQueryEntity.ExpiryDate = this.PreviousPage.ExpiryDate;
                    if (this.PreviousPage.History == "N")
                    {
                        inventoryQueryEntity.QTY = " > 0";
                    }
                }
                else
                {
                    inventoryQueryEntity.OwnerID = "";
                    inventoryQueryEntity.SkuClassID = "";
                    inventoryQueryEntity.Sku = "";
                    inventoryQueryEntity.Descr = "";
                    inventoryQueryEntity.SkuGroup = "";
                    inventoryQueryEntity.ExpiryDate = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            inventoryQueryEntity.IsGetAll = false;
            inventoryQueryEntity.CurrentPage = "0";
            inventoryQueryEntity.PageSize = this.gvDailyInv.PageSize.ToString();
            inventoryQueryEntity.SortField = "SKU_CLASS_NAME";
            inventoryQueryEntity.SortDirection = "ASC";
            this.PagerFooter.PageSize = this.gvDailyInv.PageSize;
            this.SetInventoryQueryEntity(inventoryQueryEntity);
            this.BindData2GridView(inventoryQueryEntity);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        public void SetInventoryQueryEntity(InventoryQueryEntity inventoryQueryEntity)
        {
            base.PageQueryEntitySession = inventoryQueryEntity;
        }

        public TNTDailyInvRptQuery PreviousPage
        {
            get
            {
                return (TNTDailyInvRptQuery) base.PreviousPage;
            }
        }
    }
}

